package com.lagou.controller;

import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.OSSObject;
import com.lagou.bean.UpLoadResult;
import com.lagou.service.FileUpLoadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

@Controller
@RequestMapping("/oss")
public class UploadController {

    @Autowired
    private FileUpLoadService  fileUpLoadService;

    @PostMapping("/upload")
    @ResponseBody
    public UpLoadResult upload(@RequestParam("file") MultipartFile  multipartFile){
        return  fileUpLoadService.upload(multipartFile);
    }

    @GetMapping("/download")
    @ResponseBody
    public void download(@RequestParam("fileName")String fileName, HttpServletResponse response) throws Exception {
        //根据图片名进行文件的下载
        fileUpLoadService.download(response.getOutputStream(),fileName);
    }


    @PostMapping("/delete")
    @ResponseBody
    public UpLoadResult upload(String filePath){
        return  fileUpLoadService.deleteFile(filePath);
    }
}
